<template>
  <div id="item-group">
    <div class="item" v-for="item in data">
      <div class="avatar" @click="lookUserInfo(item.author.loginname,$event)">
        <img v-lazy="item.author.avatar_url">
        <span class="avatar-name">{{item.author.loginname}}</span>
      </div>
      <div class="item-content">
        <div class="item-tips">
          <span class="top" v-if="item.top">置顶</span>
          <span class="good" v-if="item.good">精华</span>
          <span :class="item.tab">{{ tabName(item.tab) }}</span>
        </div>
        <div class="item-title" @click="topicDetail(item.id,$event)">{{item.title}}</div>
        <div class="item-info">
          <span class="item-time">发布于 &nbsp;{{item.create_at}}</span>
          <div class="item-count">
            <span class="browse"><i class="iconfont icon-attention"></i>{{item.visit_count}}</span>
            <span class="comment"><i class="iconfont icon-comment_light"></i>{{item.reply_count}}</span>
          </div>
        </div>
      </div>
      <div class="close" v-if="close" @click="itemClose(item.id,$event)">×</div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
  export default {
    props: {
      data: {
        type: Array,
        default: []
      },
      close: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        tab: {
          'share': '分享',
          'ask': '问答',
          'job': '招聘',
          'dev': '客户端'
        }
      }
    },
    methods: {
      tabName(key) {
        return this.tab[key]
      },
      // 查看话题详情
      topicDetail(id, event) {
        if (!event._constructed) {
          return
        }
        this.$router.push({name: 'topic-detail', params: {id: id}})
      },
      // 查看用户信息
      lookUserInfo(loginname, event) {
        if (!event._constructed) {
          return
        }
        this.$router.push({name: 'user', params: {username: loginname}})
      },
      // 关闭
      itemClose(id, event) {
        if (!event._constructed) {
          return
        }
        this.$emit('itemClose', id)
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  @import "~common/scss/const";
  @import "~common/scss/mixin";

  #item-group {
    .item {
      width: 100%;
      background-color: $color-background;
      margin-top: 8px;
      display: flex;
      padding: 12px;
      box-sizing: border-box;
      position: relative;
      &:first-child {
        margin-top: 0;
      }
      .avatar {
        flex: 0 0 70px;
        width: 70px;
        overflow: hidden;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: space-around;
        cursor: pointer;
        img {
          display: inline-block;
          flex: 0 0 40px;
          width: 40px;
          border-radius: 50%;
        }
        .avatar-name {
          display: inline-block;
          flex: 0 0 20px;
          width: 100%;
          height: 20px;
          text-align: center;
          font-size: $font-size-small;
          color: #999999;
          line-height: 20px;
          @include no-wrap;
        }
      }
      .item-content {
        flex: 1;
        margin-left: 10px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        > div {
          &.item-tips {
            width: 100%;
            flex: 0 0 25px;
            height: 25px;
            overflow: hidden;
            font-size: 0;
            margin-bottom: 5px;
            span {
              display: inline-block;
              font-size: $font-size-small;
              color: $color-text-w;
              margin-left: 5px;
              height: 20px;
              line-height: 20px;
              padding: 0 5px;
              border-radius: 4px;
              &:first-child {
                margin-left: 0;
              }
              &.top {
                background-color: #39f;
              }
              &.good {
                background-color: #ff0000;
              }
              &.share {
                background-color: #f60;
              }
              &.ask {
                background-color: #009900;
              }
              &.job {
                background-color: #3a9595;
              }
              &.dev {
                background-color: #999999;
              }
            }
          }
          &.item-title {
            line-height: 24px;
            flex: 0 0 48px;
            height: 48px;
            overflow: hidden;
            color: $color-text-bb;
            font-size: 15px;
            cursor: pointer;
            word-break: break-all;
            &:hover {
              color: $color-active;
            }
          }
          &.item-info {
            flex: 0 0 20px;
            height: 20px;
            margin-top: 10px;
            width: 100%;
            overflow: hidden;
            font-size: $font-size-small;
            color: #999999;
            line-height: 20px;
            display: flex;
            justify-content: space-between;
            .item-time {
              overflow: hidden;
              white-space: nowrap;
            }
            .item-count {
              overflow: hidden;
              white-space: nowrap;
              text-align: right;
              span {
                margin-right: 5px;
                display: inline-block;
                align-items: center;
                i {
                  margin-right: 2px;
                }
              }
            }
          }
        }
      }
      .close {
        position: absolute;
        right: 0;
        top: 0;
        font-size: $font-size-large;
        color: #999999;
        display: inline-block;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        cursor: pointer;
        &:hover {
          color: $color-active;
        }
      }
    }
  }
</style>
